package com.simple.handler.demo003.dao;

import com.simple.handler.demo003.constants.GetewayEnum;
import com.simple.handler.demo003.entities.GetewayEntity;

import java.util.HashMap;
import java.util.Map;

public class GetewayDaoImpl implements GetewayDao{

    /**
     * 初始化，将枚举中配置的handler 初始化到map中，方便获取
     */
    private static Map<Integer, GetewayEntity> getewayEntityHashMap = new HashMap<Integer, GetewayEntity>();
    static {
        GetewayEnum[] values = GetewayEnum.values();
        for (GetewayEnum value : values) {
            GetewayEntity getewayEntity = value.getGetewayEntity();
            getewayEntityHashMap.put(getewayEntity.getHandlerId(),getewayEntity);
        }
    }

    @Override
    public GetewayEntity getGetewayEntity(Integer handlerId) {
        return getewayEntityHashMap.get(handlerId);
    }


    /**
     * 获取第一个handler的配置项 枚举
     * @return
     */
    @Override
    public GetewayEntity getFirstGetewayEntity(){
        for(Map.Entry<Integer,GetewayEntity> entry : getewayEntityHashMap.entrySet()){
            GetewayEntity value = entry.getValue();
            //  没有上一个handler的就是第一个
            if(value.getPrehandlerId() == null){
                return value;
            }
        }
        return  null;
    }

}
